/* URBAN EXPLORER PROJECT
 * 
 * Author: Karthick Nanmaran (s1051713), School of Informatics, The University of Edinburgh
 * Email: karthicknanmaran@gmail.com
 * 
 * Supervisor: Dr.William Mackaness, 
 * 			   Institute of Geography,
 * 			   School of GeoSciences,
 * 			   The University of Edinburgh.
 * Email: william.mackaness@ed.ac.uk
 * 
 * This code can be found online at http://code.google.com/p/urban-explorers/
*/
package Firstdroid.Gps;
import com.google.android.maps.*;
import Firstdroid.Gps.R;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapViewer extends MapActivity {
 
	MyLocationOverlay myLocOverlay;
	Location loc;
	Dialog dialog;
	private MapController mapController;
	private MapView mapView;
	private LocationManager locationManager;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) 
	{
		super.onCreate(bundle);
		setContentView(R.layout.map);
		initMyLocation();
		mapView = (MapView) findViewById(R.id.mymap);
		mapView.setBuiltInZoomControls(true);
		mapView.setStreetView(true);
		mapController = mapView.getController();
		mapController.setZoom(19); 
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, new GeoUpdateHandler());

	}
	/**
     * Initialises the MyLocationOverlay and adds it to the overlays of the map
     */
    private void initMyLocation() {
    	//Runnable to determine when the first GPS fix was received.
        Runnable showWaitDialog = new Runnable() {
            @Override
            public void run() {
            while (loc == null) {
            // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
            }
            };
            // Create a Dialog to let the User know that we're waiting for a GPS Fix
            dialog = ProgressDialog.show(this, "Please wait...","Obtaining GPS fix...", true);
            Thread t = new Thread(showWaitDialog);
            t.start();
            while(loc !=null)
            {
            myLocOverlay = new MyLocationOverlay(this, mapView);
            myLocOverlay.enableMyLocation();
            mapView.getOverlays().add(myLocOverlay);
            //MapController mapController = mapView.getController();
            }
    }
        
	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat, lng); //Sets the center of the map to the current location
			mapController.animateTo(point); //	mapController.setCenter(point);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}
 
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}